<#include "license.ftl">
<@license/>
<#assign object = doc.object>
package ${object.@package}.gwt.mvp.view;

import com.google.gwt.core.client.GWT;
import redora.client.Persistable;
import redora.client.mvp.ClientFactory;
import redora.client.mvp.EditViewBase;
import redora.client.ui.EnumWidget;
import redora.client.ui.ListDropdown;
import redora.client.ui.RedoraResource;
import redora.client.util.Field;
import ${object.@package}.gwt.constants.Constants;
<#if doc["/object/attributes/enum[@scope='global']"][0]??>
import ${object.@package}.gwt.constants.GlobalEnumConstants;
import ${object.@package}.gwt.model.enums.*;
</#if>
import ${object.@package}.gwt.model.*;
import ${object.@package}.gwt.model.fields.${object.@name}Fields;

import java.util.HashMap;
import java.util.logging.Logger;

import static java.util.logging.Level.FINE;
import static redora.client.util.GWTViewUtil.*;
import static redora.client.util.GWTViewUtil.getViolatedMessagelabel;

public class ${object.@name}EditView extends EditViewBase {
    static Logger l = Logger.getLogger("${object.@name}EditView");

    static Constants constants = GWT.create(Constants.class);
    static RedoraResource resources = GWT.create(RedoraResource.class);
<#if doc["/object/attributes/enum[@scope='global']"][0]??>
    static GlobalEnumConstants globalEnumConstants = GWT.create(GlobalEnumConstants.class);
</#if>

    public ${object.@name}EditView(Class<? extends Persistable> cls, ClientFactory clientFactory) {
        super(cls, clientFactory);
    }

    /**
     * Adds the widgets to the form depending on the {@link #fields()}.
     * Before invoking this
     */
    @Override
    public void initForm() {
        for (Field field : fields) {
            l.log(FINE, "Adding field " + field.name);
            panel.add(getFormLabel(field));
            if (field == ${object.@name}Fields.id || field == ${object.@name}Fields.creationDate || field == ${object.@name}Fields.updateDate) {
                panel.add(getInputWidget(field, null, false, InputStyle.form));
<#list doc["/object/attributes/enum"] as att>
            } else if (field == ${object.@name}Fields.${att.@fieldName}) {
                EnumWidget widget = (EnumWidget)getInputWidget(field, InputStyle.form);
                widget.addValues(<#if att.@scope == "local">${object.@name}.</#if>${att.@class}.values());
                panel.add(widget);
</#list>
<#list doc["/object/attributes/long[@parentClass]"] as att>
            } else if (field == ${object.@name}Fields.${att.@fieldName}) {
                ListDropdown drop = new ListDropdown(${object.@package}.gwt.model.${att.@parentClass}.class, clientFactory, ${object.@package}.gwt.model.${att.@parentClass}.DefaultFinder.FindAll.ordinal(), null);
                drop.setName(field.name);
                drop.getElement().setId(field.inputID);
                drop.addStyleName(redoraResource.css().redoraFormInput());
                panel.add(drop);
</#list>
            } else {
                panel.add(getInputWidget(field, InputStyle.form));
            }
            panel.add(getViolatedMessagelabel(field));
        }

        panel.add(saveButton);
        panel.add(cancelButton);
    }
}
